﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    /// <summary>
    ///Trida spravujici ucast na akci.
    /// </summary>
    public class ManageParticipate
    {
        /// <summary>
        ///Metoda vraci kolekci, ktera obsahuje prihlasene ucastniky na akci.
        /// </summary>
        public static List<AppLib.Participate> GetParticipantsOnEvent(int EventID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Participate in db.Participates where Participate.EventID == EventID select Participate).ToList();

        }

        /// <summary>
        ///Metoda zjistuje, zda je hrac prihlaseny na akci.
        ///</summary>
        public static bool IsUserCheckedInToEvent(int EventID, int UserID)
        {
            var files = GetParticipantsOnEvent(EventID);

            bool t = true;

            if (files.Count > 0)
            {
                foreach (var item in files)
                {
                    if (item.UserID == UserID)
                    {
                        t = false;
                        break;
                    }
                }
            }

            return t;

        }
        /// <summary>
        ///Metoda provadi prihlaseni uzivatele na akci.
        ///</summary>
        public static void SignUserToEvent(int EventID, int UserID, int TeamID, AppLib.Detail newDetail)
        {
            AppLib.Participate join = new AppLib.Participate();
            var db = MidApp.LinqUse.DBConn;

            join.UserID = UserID;
            join.EventID = EventID;
            join.TeamID = TeamID;

            db.Participates.InsertOnSubmit(join);
            db.SubmitChanges();

            MidApp.ManageStock.ChangeCreditsBalance(UserID, TeamID,(int)-MidApp.ManageEvent.GetEventByID(EventID).Price);          
            ManageDetail.CreateNewDetail(newDetail, TeamID, UserID);
        }
        /// <summary>
        ///Metoda provadi odhlaseni uzivatele z akce.
        ///</summary>
        public static void ResignUserFromEvent(int EventID, int UserID, int TeamID, AppLib.Detail newDetail)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.Participate resign = (from Participate in db.Participates where Participate.EventID == EventID && Participate.UserID == UserID select Participate).First();

            db.Participates.DeleteOnSubmit(resign);
            db.SubmitChanges();

            MidApp.ManageStock.ChangeCreditsBalance(UserID, TeamID, (int)MidApp.ManageEvent.GetEventByID(EventID).Price);

            ManageDetail.CreateNewDetail(newDetail, TeamID, UserID);
        }
        /// <summary>
        ///Metoda vraci vsechny ucasti hrace v konkretnim tymu.
        ///</summary>
        public static List<AppLib.Participate> GetUserParticipationInTeam(int teamID, int userID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Participate in db.Participates where Participate.TeamID == teamID && Participate.UserID == userID select Participate).ToList();
        }
        /// <summary>
        ///Metoda vraci vsechny ucasti vsech hracu v konkretnim tymu.
        ///</summary>
        public static List<AppLib.Participate> GetTeamParticipation(int teamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Participate in db.Participates where Participate.TeamID == teamID select Participate).ToList();
        }            
    }
}
